﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

using YouHP.Model;
using YouHP.DALFactory;
using YouHP.Utility;

namespace YouHP.BLL
{
    public class TreatmentBLL
    {
        #region 单件模式
        static TreatmentBLL _Instance;

        public static TreatmentBLL Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new TreatmentBLL();
                }
                return _Instance;
            }
        }
        #endregion

        #region 基本添加删除修改
        public bool AddTreatment(TreatmentInfo info)
        {
            return DataAccess.TreatmentDAL.AddTreatment(info);
        }

        public bool DeleteTreatment(int[] itemIds)
        {
            return DataAccess.TreatmentDAL.DeleteTreatment(itemIds);
        }

        public bool UpdateTreatment(TreatmentInfo info)
        {
            return DataAccess.TreatmentDAL.UpdateTreatment(info);
        }

#if GENDATA
        public TreatmentInfo GetTreatmentInfo(int id)
        {
            TreatmentInfo info = new TreatmentInfo();
            info.Id = 10;
            info.Name = "治疗";
            info.Intro = "治疗介绍";
            info.Desp = "治疗描述";
            return info;
        }
#else
        public TreatmentInfo GetTreatmentInfo(int id)
        {
            TreatmentInfo info =  DataAccess.TreatmentDAL.GetTreatmentInfo(id);
            if (info != null)
            {
                if (info.IsQuantified > 0)
                {
                    //解析上下限字符串
                    string[] limitValues = info.LimitValues.Split('@');
                    if (limitValues.Length >= 2)
                    {
                        info.MinValue = Convert.ToDouble(limitValues[0]);
                        info.MaxValue = Convert.ToDouble(limitValues[1]);
                    }
                }
            }

            return info;
        }
#endif
        #endregion

        #region 治疗列表与检索
        public List<TreatmentInfo> GetTreatmentList(int pageSize, int pageIndex,out int count)
        {
            return DataAccess.TreatmentDAL.GetTreatmentList(pageSize, pageIndex,  "", out count);
        }

        public List<TreatmentInfo> GetTreatmentByPinYin(int pageSize, int pageIndex,string pinYin, out int count)
        {
            return DataAccess.TreatmentDAL.GetTreatmentByPinYin(pageSize, pageIndex, pinYin,out count);
        }

        public List<TreatmentInfo> GetTreatmentByType(int pageSize, int pageIndex, int treatmentTypeId, out int count)
        {
            return DataAccess.TreatmentDAL.GetTreatmentByType(pageSize, pageIndex, treatmentTypeId, out count);
        }

        public List<TreatmentInfo> GetTreatmentByName(int pageSize, int pageIndex, string keyword, out int count)
        {
            return DataAccess.TreatmentDAL.GetTreatmentByName(pageSize, pageIndex, keyword, out count);
        }

        /*检索后台管理所需要的所有治疗列表*/
        public List<TreatmentInfo> GetAllTreatmentByName(string keyword)
        {
            int count = 0;
            return DataAccess.TreatmentDAL.GetTreatmentByName(Constants.maxPageSize, 1, keyword, out count);
        }

#if GENDATA
        public List<TreatmentInfo> GetTreatmentList(int pageSize, int pageIndex, TreatmentSearchCondition tsCond, out int count)
        {
            count = 10;
            List<TreatmentInfo> lst = new List<TreatmentInfo>();
            for (int i = 1; i <= pageSize; i++)
            {
                TreatmentInfo info = this.GetTreatmentInfo(i);
              
                lst.Add(info);
            }
            return lst;
        }
#else
        /// <summary>
        /// 根据条件获取治疗列表
        /// </summary>
        public List<TreatmentInfo> GetTreatmentList(int pageSize, int pageIndex, TreatmentSearchCondition tsCond, out int count)
        {
            if (tsCond.FirstLetter.Length > 0)
            {
                return DataAccess.TreatmentDAL.GetTreatmentList(pageSize, pageIndex, tsCond.ToSqlConditonString(), out count);
            }

            if (tsCond.DiseaseId > 0)
            {
                return DataAccess.TreatmentDAL.GetTreatmentListByDisease(pageSize, pageIndex, tsCond.ToSqlConditonString(), out count);
            }
            else if (tsCond.TreatmentName.Length > 0)
            {
                return DataAccess.TreatmentDAL.GetTreatmentList(pageSize, pageIndex, tsCond.ToSqlConditonString(), out count);
            }

            return DataAccess.TreatmentDAL.GetTreatmentList(pageSize, pageIndex, tsCond.ToSqlConditonString(), out count);
        }
        public List<TreatmentInfo> GetTreatmentListWithOrder(int pageSize, int pageIndex, TreatmentSearchCondition tsCond, string sOrder, out int count)
        {
            if (tsCond.FirstLetter.Length > 0)
            {
                return DataAccess.TreatmentDAL.GetTreatmentListWithOrder(pageSize, pageIndex, tsCond.ToSqlConditonString(), sOrder, out count);
            }

            if (tsCond.DiseaseId > 0)
            {
                return DataAccess.TreatmentDAL.GetTreatmentListByDisease(pageSize, pageIndex, tsCond.ToSqlConditonString(),out count);
            }
            else if (tsCond.TreatmentName.Length > 0)
            {
                return DataAccess.TreatmentDAL.GetTreatmentListWithOrder(pageSize, pageIndex, tsCond.ToSqlConditonString(), sOrder, out count);
            }

            return DataAccess.TreatmentDAL.GetTreatmentListWithOrder(pageSize, pageIndex, tsCond.ToSqlConditonString(), sOrder, out count);
        }
        public List<TreatmentInfo> GetTreatmentListOrderByDiseaseId(int iDiseaseid,int pageSize, int pageIndex , out int count)
        {
            return DataAccess.TreatmentDAL.GetTreatmentListOrderByDiseaseId(iDiseaseid,pageSize, pageIndex, out count);
        }
#endif

        #endregion

        #region 治疗类型
        public bool AddTreatmentType(TreatmentTypeInfo info)
        {
            return DataAccess.TreatmentDAL.AddTreatmentType(info);
        }

        public bool DelTreatmentType(int[] itemIds)
        {
            return DataAccess.TreatmentDAL.DelTreatmentType(itemIds);
        }

        public bool UpdateTreatmentType(TreatmentTypeInfo info)
        {
            return DataAccess.TreatmentDAL.UpdateTreatmentType(info);
        }

        public TreatmentTypeInfo GetTreatmentTypeInfo(int id)
        {
            return DataAccess.TreatmentDAL.GetTreatmentTypeInfo(id);
        }

        public List<TreatmentTypeInfo> GetTreatmentTypeList()
        {
            return DataAccess.TreatmentDAL.GetTreatmentTypeList();
        }

        public List<TreatmentTypeInfo> GetTreatmentTypeList(int parentId)
        {
            return DataAccess.TreatmentDAL.GetTreatmentTypeList(parentId);
        }
        #endregion

        #region 治疗图表统计

#if GENDATA
        public List<TreatmentStatDisease> GetTreatmentStatDisease(int tid)
        {
            List<TreatmentStatDisease> lst = new List<TreatmentStatDisease>();
            for (int i = 1; i <= 10; i++)
            {
                TreatmentStatDisease stat = new TreatmentStatDisease();
                stat.DiseaseId = i;
                stat.DiseaseName = "疾病-" + i.ToString();
                stat.PatientCount = i * 10 + i;
                lst.Add(stat);
            }
            return lst;
        }
#else
        public List<TreatmentStatDisease> GetTreatmentStatDisease(int tid)
        {
            List<TreatmentStatDisease> lst = new List<TreatmentStatDisease>();
            return lst;
        }
#endif


#if GENDATA
        /// <summary>
        /// 取得病人剂量统计信息
        /// </summary>
        public List<TreatmentStatDosage> GetTreatmentStatDosage(int tid)
        {
            List<TreatmentStatDosage> lst = new List<TreatmentStatDosage>();
            for (int i = 1; i <= 10; i++)
            {
                TreatmentStatDosage stat = new TreatmentStatDosage();
                stat.Dosage = (i*10).ToString();
                stat.PatientCount = i * 10 + i;
                lst.Add(stat);
            }
            return lst;
        }
#else
        public List<TreatmentStatDosage> GetTreatmentStatDosage(int tid)
        {
            return DataAccess.TreatmentDAL.GetTreatmentStatDosage(tid);
        }
#endif

#if GENDATA
        /// <summary>
        /// 取得病人服用时间人数统计
        /// </summary>
        public List<TreatmentStatTaketime> GetTreatmentStatTaketime(int tid)
        {
            List<TreatmentStatTaketime> lst = new List<TreatmentStatTaketime>();
            for (int i = 1; i <= 10; i++)
            {
                TreatmentStatTaketime stat = new TreatmentStatTaketime();
                stat.Taketime = (i).ToString() + " 个月";
                stat.PatientCount = i * 10 + i;
                lst.Add(stat);
            }
            return lst;
        }
#else
        public List<TreatmentStatTaketime> GetTreatmentStatTaketime(int tid)
        {
            return DataAccess.TreatmentDAL.GetTreatmentStatTaketime(tid);
        }
#endif

#if GENDATA
        public List<TreatmentStartReason> GetTreatmentStartReason(int tid)
        {
            List<TreatmentStartReason> lst = new List<TreatmentStartReason>();

            TreatmentStartReason reason;

            reason = new TreatmentStartReason();
            lst.Add(reason);
            reason.StartReason = "治疗本病";
            reason.PatientCount = 10;

            reason = new TreatmentStartReason();
            lst.Add(reason);
            reason.StartReason = "治疗本病或其他疾病";
            reason.PatientCount = 12;


            reason = new TreatmentStartReason();
            lst.Add(reason);
            reason.StartReason = "保健";
            reason.PatientCount = 14;

            reason = new TreatmentStartReason();
            lst.Add(reason);
            reason.StartReason = "其他或不清楚";
            reason.PatientCount = 10;

            return lst;
        }
#else
        public List<TreatmentStartReason> GetTreatmentStartReason(int tid)
        {
            return new List<TreatmentStartReason>();
        }
#endif

#if GENDATA        
        public List<TreatmentStopReason> GetTreatmentStopReason(int tid)
        {
            List<TreatmentStopReason> lst = new List<TreatmentStopReason>();

            TreatmentStopReason reason;

            reason = new TreatmentStopReason();
            lst.Add(reason);
            reason.StopReason = "没有效果";
            reason.PatientCount = 10;

            reason = new TreatmentStopReason();
            lst.Add(reason);
            reason.StopReason = "副作用很大";
            reason.PatientCount = 12;


            reason = new TreatmentStopReason();
            lst.Add(reason);
            reason.StopReason = "其他";
            reason.PatientCount = 14;

            reason = new TreatmentStopReason();
            lst.Add(reason);
            reason.StopReason = "康复";
            reason.PatientCount = 10;

            return lst;
        }
#else
        public List<TreatmentStopReason> GetTreatmentStopReason(int tid)
        {
            return new List<TreatmentStopReason>();
        }
#endif
        #endregion
    }
}
